package com.first.course.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.first.common.pojo.EduSubject;
import com.first.common.vo.Result;
import com.first.course.mapper.EduSubjectMapper;
import com.first.course.service.EduCourseService;
import com.first.course.service.EduSubjectService;
import com.first.course.vo.EduSubjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: first-project
 * @description:
 * @author: 路志欣
 * @create: 2023-05-27 16:13
 **/
@Service
public class EduSubjectServiceImpl implements EduSubjectService {
    @Autowired
    private EduSubjectMapper eduSubjectMapper;

    @Autowired
    private EduCourseService eduCourseService;
    /**
     *
     * @param pageCurrent
     * @param pageSize
     * @return 分页模糊查询
     */
    @Override
    public Page<EduSubject> findSubject(Integer pageCurrent, Integer pageSize, EduSubjectVo eduSubjectVo) {
        Page<EduSubject> page = new Page<>(pageCurrent, pageSize);
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        if (eduSubjectVo.getTitle() != null){
            wrapper.like("title",eduSubjectVo.getTitle());
        }
        if (StringUtils.hasText(eduSubjectVo.getStatusId())){
            wrapper.eq("status_id",eduSubjectVo.getStatusId());
        }
        Page<EduSubject> page1 = eduSubjectMapper.selectPage(page, wrapper);
        List<EduSubject> records = page1.getRecords();
        List<EduSubject> list = eduSubjectMapper.selectList(null);

        for (EduSubject item : records){
          List<EduSubject> children = getChildren(item,list);
         item.setChildren(children);
        }
        return page1;
    }

    private List<EduSubject> getChildren(EduSubject item,List<EduSubject> list){
        List<EduSubject> children = new ArrayList<>();
        for (EduSubject eduSubject : list){
            if (item.getId().equals(eduSubject.getParentId())){
                children.add(eduSubject);
            }
        }
        for (EduSubject child : children) {
            child.setChildren(getChildren(child,list));
        }
        return children;
    }

    /**
     *
     * @param id
     * @param statusId
     * @param parentId
     * @return 修改状态
     */
    @Override
    public Integer ChangeStatusId(String id, String statusId,String parentId) {
        EduSubject eduSubject = new EduSubject();
        eduSubject.setId(id);
        eduSubject.setStatusId(statusId);
        eduSubject.setGmtModified(LocalDateTime.now());
        eduSubject.setParentId(parentId);
        int update = eduSubjectMapper.updateById(eduSubject);
        return update;
    }

    /**
     *
     * @param eduSubject
     * @return 添加分类
     */
    @Override
    public Integer insertsubject(EduSubject eduSubject) {
        EduSubject eduSubject1;
        if(StringUtils.hasText(eduSubject.getId())){
            eduSubject1 = new EduSubject(IdUtil.getSnowflakeNextId() + "", eduSubject.getTitle(), eduSubject.getId(), eduSubject.getStatusId(), eduSubject.getRemark(), eduSubject.getSort(), LocalDateTime.now(), LocalDateTime.now(), new ArrayList<>());
        }else {
            eduSubject1 = new EduSubject(IdUtil.getSnowflakeNextId() + "", eduSubject.getTitle(), "0", eduSubject.getStatusId(), eduSubject.getRemark(), eduSubject.getSort(), LocalDateTime.now(), LocalDateTime.now(), new ArrayList<>());
        }
        Integer insertsubject = eduSubjectMapper.insert(eduSubject1);
        return insertsubject;
    }

    /**
     *
     * @param eduSubject
     * @return 修改分类
     */
    @Override
    public Integer updatesubject(EduSubject eduSubject) {
        int update = eduSubjectMapper.updateById(eduSubject);
        return update;
    }

    /**
     * @param id
     * @return 删除
     */
    @Override
    public Integer deletesubject(String id) {
        int deleteById = eduSubjectMapper.deleteById(id);
        return deleteById;
    }


}
